define(["jquery"], function($) {
	
	return function(params) {
		var url = params.url;
		var dates = new Array();
		var hints = new Array();

		function addDate(date) {
		    if (jQuery.inArray(date, dates) < 0) 
		        dates.push(date);
		}

		function removeDate(index) {
		    dates.splice(index, 1);
		}

		// Adds a date if we don't have it yet, else remove it
		function addOrRemoveDate(date) {
		    var index = jQuery.inArray(date, dates);
		    if (index >= 0) 
		        removeDate(index);
		    else 
		        addDate(date);
		    $("#selectedDates").val(dates.join("/"));
		}

		// Takes a 1-digit number and inserts a zero before it
		function padNumber(number) {
		    var ret = new String(number);
		    if (ret.length == 1) 
		        ret = "0" + ret;
		    return ret;
		}
		
		function highlightDays(date) {
			var year = date.getFullYear();
            var month = padNumber(date.getMonth() + 1);
            var day = padNumber(date.getDate());
            var dateString = year + month + day;
            var gotDate = jQuery.inArray(dateString, dates);
            if (gotDate >= 0) {
                // Enable date so it can be deselected. Set style to be highlighted
                return [true, "ui-state-highlight",""];
            }
            // Dates not in the array are left enabled, but with no extra style
            return [true,"", ""];
        }	        
		
		$("#datepicker").datepicker({
			changeMonth:false,
            changeYear:false,
            dateFormat:"yymmdd",
            beforeShowDay:highlightDays,
            onSelect: function (dateText, inst) {
                addOrRemoveDate(dateText);
            }
		});
		
//		$("#addHoliday").on("click", function() {
//			var midurl = "";
//			var $calendar = $("#calendar_id").val();
//			var $category = $("#category").val();
//			var $start = $("#start_time").val();
//			var $end = $("#end_time").val();
//			
//			midurl = "/" + $calendar + "/" + $category + "/" + $start + "/" + $end;
//			if (dates != null) {
//				for (var i = 0; i < dates.length; i++) {
//					midurl += "/" + dates[i];
//				}
//			}
//			$.get(url+midurl, function(rows) {
//				var $table = $("#calendarDates>tbody");
//				$table.empty();
//				alert(1);
//				for (var i = 0; i < rows.length; i++) {
//					var row = "<tr>";
//					row += "<td>" + rows[i].date + "</td>";
//					row += "<td>" + rows[i].category + "</td>";
//					row += "<td>" + rows[i].start + "</td>";
//					row += "<td>" + rows[i].end + "</td>";
//					row += "</tr>";
//					$table.append(row);
//				}
//			});
//			dates = [];
//			hints = [];
//			$("#datepicker").datepicker("refresh");
//		});
	}
});